//  ========== 
//  = 工具类 JS = 
//  ========== 
window.Util || (window.Util = {});
Util.browser = {
		/**
		 * 获取URL地址栏参数值
		 * name 参数名
		 * url [optional,default=当前URL]URL地址
		 * @return {String} 参数值
		 */
		getParameter : function(name, url) {
			var paramStr = url || window.location.search;
			paramStr = paramStr.split('?')[1];
			if ((!paramStr)||paramStr.length == 0) {return null;}
			var params = paramStr.split('&');
			for ( var i = 0; i < params.length; i++) {
				var parts = params[i].split('=', 2);
				if (parts[0] == name) {
					if (parts.length < 2 || typeof (parts[1]) === "undefined"
							|| parts[1] == "undefined" || parts[1] == "null")
						return '';
					return parts[1];
				}
			}
			return null;
		},
        isPc : function(){
            //判断浏览器是否在手机端 还是pc端  pc端返回true
            var userAgentInfo = navigator.userAgent,flag = true;
            var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone","iPad", "iPod"];
            for(var n in Agents){
                if(userAgentInfo.indexOf(Agents[n])>-1){
                    flag = false;
                }
            }
            return flag;
        }
}
Util.handlebars = {
		/**
		 * Handlebars数据渲染
		 * @param obj
		 * @param temp
		 * @param data
		 */
		loadTemp : function(obj, temp, data) {
			var template = Handlebars.compile((typeof(temp) == "object")?temp.html():temp);
			obj = (typeof(obj) == "object")?obj:$(obj);
			obj.html(template(data));
		}
}
Util.isLogin = {
    /**
     * 获取登录
     * @returns {boolean}
     */
    login: function(){
        var flag = false;
        Util.ajax.postJsonAsync("front/sh/auth!getUserInfo?uid=1001","",function(json,status){
            if(!(json.bean && json.bean.userId)){
                window.sessionStorage.removeItem("userInfo");
                flag = false;
            }else{
                //存储用户信息,关闭浏览器即清除
                window.sessionStorage.setItem("userInfo",JSON.stringify(json.bean));
                flag = true;
            }
        });
        return flag;
    },
    /**
     * 获取登录后的个人信息
     */
    userInfo: function(){
        var user = window.sessionStorage.getItem('userInfo');
        var userInfo = JSON.parse(user);
        return userInfo;
    },
    /*
    * 跳转登录页面
    *
    */
    toLogin: function(){
        /*特殊符号转码*/
        var preUrl = encodeURIComponent(window.location.pathname + window.location.search);
        /*远程*/
        /*window.location.href = '/release/module/login.html?preUrl='+preUrl;*/
        /*本地*/
        window.location.href = location.pathname.split('module/')[0]+'module/login.html?preUrl='+preUrl;
    },
    /**
     * 登陆后返回上一页(包含arr的单独处理)
     * @param arr 数组,字符串
     * @param url 单独处理返回连接
     * @returns {*}
     */
    skipUrl: function(arr,url){
        var arg = [];
        var skipUrl = Util.browser.getParameter('preUrl');
        if(skipUrl == null){
            return'../module/member/memberHome.html';
        }else{
            if(!arr){
                return decodeURIComponent(Util.browser.getParameter('preUrl'));
            }else{
                if(Object.prototype.toString.call(arr) === '[object Array]'){
                    arg = arr;
                }else{
                    arg = arr.split(",");
                }
                for (var i = 0,len = arg.length; i < len; i++) {
                    if (skipUrl.indexOf(arg[i]) != -1){
                        return window.location.href = url;
                    }
                }
                return decodeURIComponent(Util.browser.getParameter('preUrl'));
            }
        }
    }
};
Util.localstorage = {
    /**
     * 保存本地存储
     * @param key
     * @param value
     */
    setParam : function(key, value){
        localStorage.setItem(key, value);
    },
    /**
     * 获得本地存储
     * @param key
     * @returns {null}
     * 查询不存在的key时，有的浏览器返回undefined，这里统一返回null
     */
    getParam : function(key){
        var v = localStorage.getItem(key);
        return v === undefined ? null : v;
    },
    /**
     * 清除KEY
     * @param key
     */
    removeParam : function(key){
        localStorage.removeItem(key);
    },
    /**
     * 清除本地存储
     */
    clearParam : function(){
        localStorage.clear();
    }
}
Util.layer = {
    tips : function(obj,times){
       return layer.open({
            content:obj,
            time :times ? times : 1,
            style :"background:rgba(255,255,255,1);color:#555;min-width:150px;"
        });
    },
    confirm : function(opt){
        //确认提示
        return layer.open({
            title : opt.title,
            content : opt.content,
            btn : opt.btn? opt.btn :['确认','取消'],
            yes : opt.okFn,
            no  : opt.cancelFn,
            cancel : opt.closeFn
        });
    },
    bottomConfirm : function(opt){
        //从底部弹出
        return layer.open({
            content : opt.content,
            skin : 'footer',
            btn : opt.btn? opt.btn :['删除','取消'],
            yes : opt.okFn,
            no  : opt.cancelFn,
            cancel : opt.closeFn
        });
    },
    bottomTips : function(opt){
        //底部提示
        return layer.open({
            content : opt.content,
            skin : 'footer'
        });
    },
    opendiv : function(opt){
        //弹出框
        return layer.open({
            type : 1,
            content : opt.content,
            anim : opt.anim ? opt.anim : 0,
            style : opt.style ? opt.style : "position:fixed;bottom:0;width:100%;height:50%;padding:10px 0;border:none;display:block;",
            className : opt.className ? opt.className : ""
        });
    },
    prompt : function(opt){
        return layer.open({
            title : opt.title ? opt.title :"",
            content : opt.content
        });
    },
    loading : function(times){
        return layer.open({
            /*type : 2,*/
            content: '<div class="t-loading-cmcc-lg">加载中...</div>',
            style :"background:rgba(255,255,255,1);color:#777;",
            shade: true,
            time : times ? times : ""
        })
    },
    close : function(index){
        layer.close(index);
    },
    closeAll : function (){
        layer.closeAll();
    }
}
Util.page = {
    /**
     * 页面状态显示无数据、无网络等
     * @param param
     * @param callback
     */
    state: function(param,callback){
        var stateParam = {
            icon: 'icon-data-empty',   //@param : icon 图标
            el: '',     //@param : el 插入的DOM节点
            txt: '',    //@param : txt 插入的描述语句
            btn: false, //@param : btn 是否需要按钮
            btnCon:''  //@param : con 插入的内容
        };
        stateParam = $.extend(stateParam,param);
        var area = (typeof  stateParam.el == 'object') ? stateParam.el : $(stateParam.el),
            txt = stateParam.txt ? stateParam.txt : '这里什么也没有',
            htmlBox = '';
        htmlBox +='<div class="pageState">';
        htmlBox +='<i class="iconfont '+ (stateParam.icon ? stateParam.icon : 'icon-data-empty') +'"></i>';
        htmlBox +='<div class="txt">'+ txt +'</div>';
        if(stateParam.btn){
            htmlBox +='<a class="btn-primary stateBtn">'+ (stateParam.btnCon ? stateParam.btnCon : '立即去选购') +'</a>';
        }
        htmlBox +='</div>';
        area.html(htmlBox);
        $('.stateBtn').tap(function(){
            if(typeof callback == 'function'){
                callback();
            }
        })
    }
};
